//
//  NWWelcomeViewController.swift
//  Weibo
//
//  Created by Northwan on 16/1/23.
//  Copyright © 2016年 Northwan. All rights reserved.
//

import UIKit
import SDWebImage

class NWWelcomeViewController: UIViewController {
    override func loadView() {
        view = backgroundView
        setupUI()
    }
    
    private func setupUI(){
        view.addSubview(headView)
        headView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(200)
        }
        view.addSubview(messageLabel)
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headView)
            make.top.equalTo(headView.snp_bottom).offset(15)
        }
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        headView.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(view).offset(150)
        }
        
        messageLabel.alpha = 0
        //Damping:阻尼效果 0-1
        //initialSpringVelocity:执行动画的初始速度
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
                self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(1, animations: { () -> Void in
                    self.messageLabel.alpha = 1
               }, completion: { (_) -> Void in
                //跳转到主界面
                NSNotificationCenter.defaultCenter().postNotificationName(kSwithRootCtrlNortif, object: self)
            })
        }
    }
    
    private lazy var backgroundView:UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "ad_background"))
        return imageView
    }()
    
    private lazy var headView:UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        imageView.cornerRadius = 45
        guard let headUrl =  NWUserAccountViewModel.sharedAccount.userAccount?.avatar_large else{
            return imageView
        }
        let url = NSURL(string: headUrl)
        imageView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
        imageView.size = CGSize(width: 90, height: 90)
        return imageView
    }()
    
    private lazy var messageLabel:UILabel = {
        let label = UILabel()
        label.font = UIFont.boldSystemFontOfSize(16)
        label.textColor = UIColor.darkGrayColor()
        label.text = "欢迎回来"
        return label
    }()
}
